﻿#Region "Copyright/License"

'  Copyright (C) 2011, 2012, 2013 Paycircuit.com, Thinking Spot, Ed Chavez
'  http://goo.gl/jty8r
' 
'  Licensed under the Apache License, Version 2.0 (the "License");
'  you may not use this file except in compliance with the License.
'  You may obtain a copy of the License at
' 
'       http://www.apache.org/licenses/LICENSE-2.0
' 
'  Unless required by applicable law or agreed to in writing, software
'  distributed under the License is distributed on an "AS IS" BASIS,
'  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'  See the License for the specific language governing permissions and
'  limitations under the License.

#End Region

Imports System.ComponentModel.DataAnnotations
Imports System.Runtime.Serialization

Namespace google.iap

    <DataContract()>
    Public Class InAppItemRequestObject
        Inherits RequestObject

#Region "Defaults"

        Private _currencyCode As String = "USD"
        Private _price As String = "0.00"
        Private _sellerData As String = Nothing
        Private _name As String = String.Empty
        Private _description As String = Nothing

#End Region

        ''' <summary>
        ''' Required. A 3-character currency code that defines the billing currency. Currently the only supported currency code is USD.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Required()>
        <StringLength(3, ErrorMessage:="Maximum of 3 characters")>
        <DataMember(isRequired:=True, EmitDefaultValue:=True, Order:=1)>
        Public Overrides Property currencyCode As String
            Get
                Return _currencyCode
            End Get
            Set(value As String)
                If Not String.IsNullOrEmpty(value) AndAlso maxLength(3, value.Length) Then
                    _currencyCode = value
                Else
                    Throw New ArgumentOutOfRangeException("InAppItemRequestObject currencyCode", "Required. Maximimum of 3 characters")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Required. The purchase price of the item, with up to 2 decimal places.
        ''' Google will reject value exceeding 2 decimal places.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Required()>
        <DataMember(isRequired:=True, EmitDefaultValue:=True, Order:=2)>
        Public Overrides Property price As String
            Get
                Return _price
            End Get
            Set(value As String)
                'NOTE: Pending official clarification.
                '   Currently Google sandbox allows 0 value for itemPrice.
                '   While it would be odd to allow 0 value payments, allow 0 value to match official Google spec/current behavior.
                If Not String.IsNullOrEmpty(value) AndAlso IsNumeric(value) AndAlso CDec(value) >= 0 Then
                    _price = value
                Else
                    Throw New ArgumentOutOfRangeException("InAppItemRequestObject price", "Item price is required.")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Optional: Data to be passed to your success and failure callbacks. The string can have no more than 200 characters.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <StringLength(200, ErrorMessage:="Maximum of 200 characters")>
        <DataMember(EmitDefaultValue:=False, isRequired:=False, Order:=4)>
        Public Overrides Property sellerData As String
            Get
                Return _sellerData
            End Get
            Set(value As String)
                If maxLength(200, value.Length) Then
                    _sellerData = value
                Else
                    Throw New ArgumentOutOfRangeException("InAppItemRequestObject sellerData", "Maximimum of 200 characters")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Required. The name of the item. This name is displayed prominently in the purchase flow UI and can have no more than 50 characters.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Required()>
        <StringLength(50, ErrorMessage:="Maximum of 50 characters")>
        <DataMember(isRequired:=True, Order:=3)>
        Public Overrides Property name As String
            Get
                Return _name
            End Get
            Set(value As String)
                If Not String.IsNullOrEmpty(value) AndAlso maxLength(50, value.Length) Then
                    _name = value
                Else
                    Throw New ArgumentOutOfRangeException("InAppItemRequestObject name", "Required. Maximimum of 50 characters")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Optional: Text that describes the item. This description is displayed in the purchase flow UI and can have no more than 100 characters.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <StringLength(100, ErrorMessage:="Maximum of 100 characters")>
        <DataMember(EmitDefaultValue:=False, isRequired:=False, Order:=5)>
        Public Overrides Property description As String
            Get
                Return _description
            End Get
            Set(value As String)
                If maxLength(100, value.Length) Then
                    _description = value
                Else
                    Throw New ArgumentOutOfRangeException("InAppItemRequestObject description", "Maximimum of 100 characters")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Refer to property Intellisense documentation/information/hints for requirements (will throw exception).
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()

        End Sub

        Private Function maxLength(ByVal maxCharacterLength As Integer, ByVal currentLength As Integer) As Boolean
            Return currentLength <= maxCharacterLength
        End Function

        <OnDeserialized()>
        Public Sub validateDeserializedClaim(context As System.Runtime.Serialization.StreamingContext)
            validateClaim()
        End Sub

        <OnSerialized()>
        Public Sub validateSerializedClaim(context As System.Runtime.Serialization.StreamingContext)
            validateClaim()
        End Sub

        Private Sub validateClaim()
            If String.IsNullOrEmpty(Me.name) OrElse String.IsNullOrEmpty(Me.price) OrElse String.IsNullOrEmpty(Me.currencyCode) Then
                Throw New NullReferenceException("Invalid InAppItemRequestObject - make sure you provide all required properties")
            End If
        End Sub

    End Class

End Namespace